$(function(){
	var apartmentObject;
	var floor_flag=true;
	//表格折叠/展开
    $("#label-floor").click(function(){
        if(floor_flag){
            $("#div-floor").hide();
            floor_flag=false;
        }else{
            $("#div-floor").show();
            floor_flag=true;
        }
    });
	//恢复默认样式
	$("input").click(function(){
		$(this).removeClass('error');
		var val=$(this).attr("id");
		var label_id="#"+val+"-error";
		$(label_id).hide();
	});
	//恢复默认样式,动态添加的元素使用
	$("#display-floor").delegate("input","click",function(){
		$(this).removeClass('error');
		var val=$(this).attr("id");
		var label_id="#"+val+"-error";
		$(label_id).hide();
	});

	//未使用
	//保存楼层设置
	//保存单层设置
	saveFloor=function(floor){
		var floorObject=getFloor(floor);
		floorJSON=JSON.stringify(floorObject);
		$.post("./php/apartment-edit.php",{action:"2",data:floorJSON},function(jsonString){
			jsonObject=JSON.parse(jsonString);
			switch(parseInt(jsonObject.code)){
				case 2001:break;
				case 2002:break;
				case 2003:break;
				case 2004:break;
				default:break;
			}
			
		});
		//alert(floorJSON);
	}
	//保存全部
	function saveAll(){

	}

	//修改公寓信息(保存)
	$("#btn-alter").click(function(){
		if(true){//validate()
			var apartment=getApartment();
			apartmentJSON=JSON.stringify(apartment);
			//alert(apartmentJSON);
			$.post("./php/apartment-edit.php",{action:"2",data:apartmentJSON},
			function(jsonString){
				//alert(jsonString);
				jsonObject=JSON.parse(jsonString);
				switch(jsonObject.code){
					case '2001':
						$.Huimodalalert('修改成功',2000);
					break;
					case '2002':break;
					case '2003':
						validateServer(jsonObject,1);
					break;
					default:break;
				}
			});
		}
	});

	//获取页面信息
	//获取url参数
	function GetQueryString(name){
		var reg = new RegExp("(^|&)"+name+"=([^&]*)(&|$)");
		var r = window.location.search.substr(1).match(reg);
		if(r!=null)return unescape(r[2]); return null;
	}
	var parameter=new Object();
	var myurl=GetQueryString("id");
	parameter.id=myurl;
	if(myurl !=null && myurl.toString().length>=1){
		//请求公寓信息
		$.post("./php/apartment-edit.php",{action:1,data:JSON.stringify(parameter)},function(jsonString){
			apartmentObject=JSON.parse(jsonString);
			apartmentObject.old_id=apartmentObject.id;
			displayIndex(apartmentObject);
		});
	}

	//测试版本
	function beta(){
		var parameter=new Object();
		parameter.id="1";
		$.post("./php/apartment-edit.php",{action:1,data:JSON.stringify(parameter)},function(jsonString){
			//alert(jsonString);
		});
	}
	//获取公寓基本信息
	function getApartment(){
		//apartmentArray=new Array("#id","#name","#abbreviation","#floor","#property","#remarks");
		//apartmentOld=apartmentObject.apartment;
		apartmentObj=new Object();
		apartment=new Object();

		apartment.id=$("#id").val();
		apartment.name=$("#name").val();
		apartment.abbreviation=$("#abbreviation").val();
		apartment.floor=$("#floor").val();//总共有多少层
		apartment.property=$("#property").val();
		apartment.status=$("#status").val();
		apartment.remarks=$("#remarks").val();
		
		apartmentObj.apartment=apartment;
		apartmentObj.floor=new Array();
		for(var i=0;i<$("#display-floor").find("tr").length;i++){
			apartmentObj.floor[i]=getFloor(i+1);
		}

		return apartmentObj;
	}
	//获取每层详细设置
	function getFloor(floor){
		//floorObj=apartmentObject.floor[floor];
		floorObject=new Object();
		
		floorObject.floor=floor;//在第几层
		floorObject.rooms=$("#floor_rooms"+floor).val();//
		floorObject.beds=$("#floor_beds"+floor).val();
		floorObject.property=$("#floor_property"+floor).val();
		floorObject.status=$("#floor_status"+floor).val();
		floorObject.remarks=$("#floor_remarks"+floor).val();
		
		return floorObject;
	}
	//下拉框改变选项时
	$("select").change(function(){
		var id=$(this).attr("id");
		switch(id){
			case "status":
			break;
			case "property":
				switch(parseInt($(this).val())){
					case 1:
						propertyChange("floor_property","<option value=\"1\" selected=\"\">女生</option>");
					break;
					case 2:
						propertyChange("floor_property","<option value=\"2\" selected=\"\">男生</option>");
					break;
					case 3:
						var html="<option value=\"1\" selected=\"\">女生</option>"+
							"<option value=\"2\">男生</option>"+
							"<option value=\"3\">混合</option></select></td>";
						propertyChange("floor_property",html);
					break;
					default:break;
				}
			break;
			default:break;
		}
	});
	//更改表格内的下拉框
	function propertyChange(id,html){
		for(var i=1;i<=$("#display-floor").find("tr").length;i++){
			$("#"+id+i).html(html);
		}
	}

	//当经过输入框时
	$('.skin-minimal input').iCheck({
		checkboxClass: 'icheckbox-blue',
		radioClass: 'iradio-blue',
		increaseArea: '20%'
	});
	//验证楼层设置
	function floorValidate(floor){
		if($("#floor_rooms"+floor).val()==""){
			$("#floor_rooms"+floor).addClass("error");
			$("#floor_rooms"+floor+"-error").show();
			flag=false;
		}
		if($("#floor_beds"+floor).val()==""){
			$("#floor_beds"+floor).addClass("error");
			$("#floor_beds"+floor+"-error").show();
			flag=false;
		}
		var floor_remarks="";
		floor_remarks=$("#floor_remarks"+floor).val();
		if(floor_remarks.length>30){
			$("#floor_remarks"+floor).addClass('error');
			$("#floor_remarks"+floor+"-error").show();
			flag=false;
		}
	}
	//表单验证
	function validate(){
		//前半部分
		var id_array=new Array("#id","#name","#height");
		var error_array=new Array("#id-error","#name-error","#height-error");
		var flag=true;
		for(var i=0;i<id_array.length;i++){
			if($(id_array[i]).val()==""){
				$(id_array[i]).addClass('error');
				$(error_array[i]).show();
				flag=false;
			}
		}
		var name="";
		name=$("#name").val();
		if(name.length>20)
		{
			$("#name").addClass('error');
			$("#name-error").show();
			flag=false;
		}

		var abbreviation_str="";
		var abbreviation_str=$("#abbreviation").val();
		if(abbreviation_str.length>10)
		{
			$("#abbreviation").addClass('error');
			$("#abbreviation-error").show();
			flag=false;
		}
		var remarks_str="";
		remarks_str=$("#remarks").val();
		if(remarks_str.length>30){
			$("#remarks").addClass('error');
			$("#remarks-error").show();
			flag=false;
		}

		//后半部分
		for(var i=0;i<$("#display-floor").find("tr").length;i++){
			floorValidate(i);
		}
		return flag;
	}
	//显示服务端验证信息
	function validateServer(obj,code){
		var apartmentArray=Array("编号","名称");
		var apartment_array=new Array("#id","#name","#abbreviation","#floor",
			"#property","#status","#remarks");
		var error_array=new Array("#id-error","#name-error","#abbreviation-error",
			"#floor-error","#property-error","#status-error","#remarks-error");
		var floor_array=new Array("#floor_floor","#floor_rooms","#floor_beds","#floor_property","#floor_status","#floor_remarks");
		switch(code){
			case 1:
				//使用数字下标标识错误输入的值
				/*$.each(obj.apartment, function(e, value) {
					$(apartment_array[value]).addClass('error');
					$(error_array[value]).show();
					$(error_array[value]).html("输入不符合要求");
				});*/
				//使用id标识错误输入的值
				$.each(obj.apartment,function(key,value){
					$("#"+value).addClass('error');
					$("#"+value+"-error").show();
					$("#"+value+"-error").html("输入不符合要求");
				});
				//使用数字下标
				/*var count=0;
				$.each(obj.floor,function(e, value){
					for(var i=0;i<floor_array.length;i++){
						$(floor_array[value[i]]+count).addClass('error');
						$(floor_array[value[i]]+count+"-error").show();
						$(floor_array[value[i]]+count+"-error").html("输入不符合要求");
					}
					++count;
				});*/
				//使用id标识错误输入的值
				var count=1;
				$.each(obj.floor,function(e, v){
					$.each(v,function(key,value){
						$("#floor_"+value+count).addClass('error');
						$("#floor_"+value+count+"-error").show();
						$("#floor_"+value+count+"-error").html("输入不符合要求");
					});
					++count;
				});
			break;
			case 2:
				$.each(obj, function(e, value) {
					//alert(apartment_array[value]);
					$(apartment_array[value]).addClass('error');
					$(error_array[value]).show();
					$(error_array[value]).html(array[value]+"已存在");
				});
			break;
			default:break;
		}
		//alert(message);
		
		return;
	}
	//显示页面信息
	function displayIndex(myObject){
		apartment=myObject.apartment;
		$("#id").val(parseInt(apartment.id));
		$("#name").val(apartment.name);
		$("#floor").val(parseInt(apartment.floor));
		$("#abbreviation").val(apartment.abbreviation);
		$("#property").val(parseInt(apartment.property));
		$("#status").val(parseInt(apartment.status));
		$("#remarks").val(apartment.remarks);
		displayFloor(myObject.floor);
	}
	//显示楼层信息
    function displayFloor(obj){
        var str="";
		$.each(obj, function(e, floor) {
			str+=handleFloor(floor);
		});
        $("#display-floor").html(str);
	}
	
	//处理
	function handleFloor(obj){
		var str="<tr class=\"text-c\"><td id=\"floor_floor"+obj.floor+"\">"+obj.floor+"</td>"+
			"<td><div class=\"formControls\"><input type=\"number\" id=\"floor_rooms"+obj.floor+"\" value=\""+obj.rooms+"\" class=\"input-text text-c\" style=\"width:100%;\">"+
			"<label id=\"floor_rooms"+obj.floor+"-error\" class=\"error valid\" for=\"floor_rooms"+obj.floor+"\" style=\"display:none;\">不能为空</label><div></td>"+
			"<td><div class=\"formControls\"><input type=\"number\" id=\"floor_beds"+obj.floor+"\" value=\""+obj.beds+"\" class=\"input-text text-c\" style=\"width:100%;\">"+
			"<label id=\"floor_beds"+obj.floor+"-error\" class=\"error valid\" for=\"floor_beds"+obj.floor+"\" style=\"display:none;\">不能为空</label><div></td>"+
			"<td><select id=\"floor_property"+obj.floor+"\" class=\"select\" size=\"1\" style=\"height:31px;\">";
			switch(parseInt(obj.property)){
				case 1:
				str+="<option value=\"1\" selected=\"\">女生</option>";
				break;
				case 2:
				str+="<option value=\"2\" selected=\"\">男生</option>";
				break;
				case 3:
				str+="<option value=\"1\" selected=\"\">女生</option>"+
					"<option value=\"2\">男生</option>"+
					"<option value=\"3\">混合</option></select></td>";
				break;
				default:
				str+="<option value=\"1\" selected=\"\">女生</option>"+
					"<option value=\"2\">男生</option>"+
					"<option value=\"3\">混合</option></select></td>";
				break;
			}
			
			str+="<td><select id=\"floor_status"+obj.floor+"\" class=\"select\" size=\"1\" style=\"height:31px;\">";

			switch(parseInt(obj.status)){
				case 0:
				str+="<option value=\"1\">使用</option>"+
				"<option value=\"0\" selected=\"\">停用</option></select></td>";
				break;
				case 1:
				str+="<option value=\"1\" selected=\"\">使用</option>"+
				"<option value=\"0\">停用</option></select></td>";
				break;
				default:
				str+="<option value=\"1\" selected=\"\">使用</option>"+
				"<option value=\"0\">停用</option></select></td>";
				break;
			}
			
			str+="<td><input id=\"floor_remarks"+obj.floor+"\" value=\""+obj.remarks+"\" type=\"text\" class=\"input-text\" style=\"width:100%\">"+
			"<label id=\"floor_remarks"+obj.floor+"-error\" class=\"error valid\" for=\"floor_remarks"+obj.floor+"\" style=\"display:none;\">超出字数限制</label></td>"+
			"<td><a style=\"text-decoration:none\" class=\"ml-5\" onClick=\"setFloor("+obj.floor+")\" href=\"javascript:;\" title=\"详细设置\">"+
			"<i class=\"Hui-iconfont\">&#xe6df;</i></a></tr>";
		return str;
	}
	/**
	 * "<a style=\"text-decoration:none\" class=\"ml-5\" onClick=\"saveFloor("+obj.floor+")\" href=\"javascript:;\" title=\"保存\">"+
			"<i class=\"Hui-iconfont\">&#xe632;</i></a></td>
	 *///
});